<?php

namespace App\Http\Requests\Backend;

use App\Models\School;
use App\Rules\ValidatePrice;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
 * Class EditUserRole
 * @package App\Http\Requests\Backend
 */
class EditUserRole extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'school_id' => [
                'required',
                function ($attribute, $value, $fail) {
                    if (School::query()->where('status', School::STATUS_ACTIVE)->find($value)->doesntExist()) {
                        return $fail('未找到该学校');
                    }
                }
            ],
            'name' => [
                'required',
                Rule::unique('user_roles')->where('school_id', $this->school_id)->ignore((int)trim(strrchr(Request()->path(), '/'),'/'))
            ],
            'can_breakfast' => 'required',
            'can_lunch' => 'required',
            'can_dinner' => 'required',
            'breakfast_rate' => [
                'required_if:can_breakfast,1',
                new ValidatePrice($this->can_breakfast == 1)
            ],
            'lunch_rate' => [
                'required_if:can_lunch,1',
                new ValidatePrice($this->can_lunch == 1)
            ],
            'dinner_rate' => [
                'required_if:can_dinner,1',
                new ValidatePrice($this->can_dinner == 1)
            ],
            'morning_price_rule' => [
                'required_if:is_using,1',
                new ValidatePrice($this->is_using == 1)
            ],
            'lunch_price_rule' => [
                'required_if:is_using,1',
                new ValidatePrice($this->is_using == 1)
            ],
            'dinner_price_rule' => [
                'required_if:is_using,1',
                new ValidatePrice($this->is_using == 1)
            ],
            'morning_price' => [
                'required_if:morning_price_rule,1',
                new ValidatePrice($this->morning_price_rule == 1)
            ],
            'lunch_price' => [
                'required_if:lunch_price_rule,1',
                new ValidatePrice($this->lunch_price_rule == 1)
            ],
            'dinner_price' => [
                'required_if:dinner_price_rule,1',
                new ValidatePrice($this->dinner_price_rule == 1)
            ]
        ];
    }

    /**
     * @return array
     */
    public function attributes()
    {
        return [
            'name' => '角色名',
            'school_id' => '学校',
            'can_breakfast' => '是否能订早餐',
            'can_lunch' => '是否能订午餐',
            'can_dinner' => '是否能订晚餐',
            'breakfast_rate' => '早餐优惠比例',
            'lunch_rate' => '午餐优惠比例',
            'dinner_rate' => '晚餐优惠比例',
            'morning_price_rule' => '早餐价格规则',
            'lunch_price_rule' => '午餐价格规则',
            'dinner_price_rule' => '晚餐价格规则',
            'morning_price' => '早餐价格',
            'lunch_price' => '午餐价格',
            'dinner_price' => '晚餐价格'
        ];
    }

    /**
     * @return array
     */
    public function messages()
    {
        return [
            'school_id.required' => '请选择:attribute',
            'name.required' => '请填写:attribute',
            'name.unique' => '已存在相同的:attribute',
            'can_breakfast.required' => '请选择:attribute',
            'can_lunch.required' => '请选择:attribute',
            'can_dinner.required' => '请选择:attribute',
            'breakfast_rate.required_if' => '请填写:attribute',
            'lunch_rate.required_if' => '请填写:attribute',
            'dinner_rate.required_if' => '请填写:attribute',
            'morning_price_rule.required_if' => '请填写:attribute',
            'lunch_price_rule.required_if' => '请填写:attribute',
            'dinner_price_rule.required_if' => '请填写:attribute',
            'morning_price.required_if' => '请填写:attribute',
            'lunch_price.required_if' => '请填写:attribute',
            'dinner_price.required_if' => '请填写:attribute'
        ];
    }
}
